<script>
import { config, column } from './formConfig.js'
import BasicManage from '@/components/basic/BasicManage.vue'
import { requestApi } from '@/requset/api'
import { deepClone } from '@/plugins/clone'
export default {
    extends: BasicManage,
    data() {
        return {
            table_title: '故障类型',
            url: 'trouble',
            baseForm: {},
        }
    },
    methods: {
        isJSON(str) {
            try {
                JSON.parse(str)
                return true
            } catch (e) {
                return false
            }
        },
        add(data) {
            let { config } = data
            if (this.isJSON(config)) {
                requestApi('post', this.url + '/add', this.to(deepClone(data))).then((res) => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: '添加成功',
                        })
                        this.getlist()
                        this.$refs.zyform.closeDialog()
                    }
                })
            } else {
                this.$message.error('解析失败，不是标准的 JSON 格式')
            }
        },
        edit(data) {
            let { config } = data
            if (this.isJSON(config)) {
                requestApi('post', this.url + '/edit', this.to(deepClone(data))).then((res) => {
                    if (res.data.code == 0) {
                        this.$message({
                            type: 'success',
                            message: '编辑成功',
                        })
                        this.getlist()
                        this.$refs.zyform.closeDialog()
                    }
                })
            } else {
                this.$message.error('解析失败，不是标准的 JSON 格式')
            }
        },
    },
    created() {
        this.dialog.labelWidth = '120px'
        column[column.length - 1].buttons = [
            {
                text: '编辑',
                click: (val) => {
                    this.handleEdit(val)
                },
                icon: 'el-icon-edit',
            },
            {
                text: '删除',
                click: (val) => {
                    this.delete(val)
                },
                icon: 'el-icon-delete',
            },
        ]
        column[column.length - 1].if = this.operate
        this.dialog.config = config
        this.tableColumn = column
        this.getlist()
    },
}
</script>
